using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ArcEngine.Graphics;
using ArcEngine.Resources;
using WeifenLuo.WinFormsUI.Docking;


//
//TODO:
//- Definir le clamping de la texture (GL_CLAMP)
//- Definir le type d'affichage de la texture (GL_HINT)
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

namespace ArcEngine.Editor
{
	public partial class TextureForm : DockContent
	{
		public TextureForm()
		{
			InitializeComponent();
			GlControl.InitializeContexts();
			
		}


		/// <summary>
		/// Initialize the form
		/// </summary>
		/// <param name="texture"></param>
		/// <returns></returns>
		public bool Init(Texture texture)
		{
			//
			Texture = texture;
			PropertyBox.SelectedObject = texture;
			TabText = Texture.Name;

			// OpenGL init
			GlControl.MakeCurrent();
			Video.ShareVideoContext();
			Video.ClearColor = Color.Black;
			Video.Texturing = true;
			Video.Texture = Texture;
			Video.Blending = true;




			// Preload background texture resource
			//Assembly a = Assembly.GetExecutingAssembly();
			//Stream stream = a.GetManifestResourceStream("ArcEngine.Editor.Resources.checkerboard.png");
			//CheckerBoard = new Texture("CheckerBoard");
			//CheckerBoard.LoadImage(stream);

			CheckerBoard = new Texture("CheckerBoard");
			Stream stream = ResourceManager.GetInternalResource("ArcEngine.Files.checkerboard.png");
			CheckerBoard.LoadImage(stream);
			stream.Close();
			return true;
		}


		#region Events


		/// <summary>
		/// Paint event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GlControl_Paint(object sender, PaintEventArgs e)
		{
			Video.ClearBuffers();

			Video.Blit(CheckerBoard, new Rectangle(Point.Empty, GlControl.Size), TextureLayout.Tile);

			Video.Blit(Texture, Point.Empty);
		}


		/// <summary>
		/// Control resize
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void GlControl_Resize(object sender, EventArgs e)
		{
			GlControl.MakeCurrent();
			Video.ViewPort = new Rectangle(new Point(), GlControl.Size);
		}


		#endregion

		
		#region Variables

		/// <summary>
		/// Current texture
		/// </summary>
		Texture Texture;

		// Background texture
		Texture CheckerBoard;

		#endregion




	}
}
